package com.xqls.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.xqls.biz.DictionaryBiz;
import com.xqls.biz.SamplingRecordBiz;
import com.xqls.util.CommonPage;
import com.xqls.util.CommonResult;
import com.xqls.util.DictionaryEnums;
import com.xqls.util.GetKeyByValue;

/**
 * 在administration表中根据sampleType类型建立5条数据
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value = "/sampling")
public class SamplingRecordController {
	@Autowired
	private SamplingRecordBiz samplingBiz;
	@Autowired private DictionaryBiz dictionaryBiz;
	@RequestMapping(value = "/{tablename}/samplingList")
	public ModelAndView samplingList(@PathVariable(value = "tablename") String tablename) {
		ModelAndView view = new ModelAndView("Sampling/samplingList");
		Map<Integer, String> map = dictionaryBiz.queryDictionaryByParent(  DictionaryEnums.COMPANY_TYPE.getCode());
		String sampleType = null;
		String url = null;
		String title = null;
		if (tablename.equals("discharge")) {
			sampleType = "discharge";
			url = "/sampling/" + sampleType + "/samplingListData";
			title = "排放口采样记录";
			view.addObject("deviceType", GetKeyByValue.getKey(map, "排放口"));
		} else if (tablename.equals("rainwater")) {
			sampleType = "rainwater";
			url = "/sampling/" + sampleType + "/samplingListData";
			title = "企业雨水排放口采样记录";
			view.addObject("deviceType", GetKeyByValue.getKey(map, "企业雨水排放口"));
		} else if (tablename.equals("stormwater")) {
			sampleType = "stormwater";
			url = "/sampling/" + sampleType + "/samplingListData";
			title = "公共雨水排放口采样记录";
			view.addObject("deviceType", GetKeyByValue.getKey(map, "公共雨水排放口"));
		} else if (tablename.equals("wastegas")) {
			sampleType = "wastegas";
			url = "/sampling/" + sampleType + "/samplingListData";
			title = "废气排放口采样记录";
			view.addObject("deviceType", GetKeyByValue.getKey(map, "废气排放口"));
		}
		
		view.addObject("columns", CommonPage.setColumnToPage(null, null, url, title));
		view.addObject("title", title);
		view.addObject("isRecordList", true);
		return view;

	}

	@RequestMapping(value = "/{sampleType}/samplingListData")
	@ResponseBody
	public CommonResult samplingListDta(@RequestBody CommonPage commonPage,
			@PathVariable(value = "sampleType") String sampleType, HttpServletRequest request, HttpServletResponse response) {
		CommonResult commonResult = samplingBiz.queryRecordBySampleType(commonPage, sampleType,request,response);
		if (commonResult.getIsSuccess()) {
			commonResult.setData(commonPage);
			System.out.println(commonPage.toSHowPage());
		}

		return commonResult;
	}
	
	@RequestMapping(value = "/getExcelByMap/{params}")
	public void getExcel(HttpServletRequest request,HttpServletResponse response,@PathVariable(value = "params") String params) {
		JSONObject jsonObject=JSONObject.parseObject(params);
		this.samplingBiz.getListByMap(request, response, jsonObject);
	}
	
//排放口按钮
	@RequestMapping(value = "/radioDischarge/{deviceNo}")
	public ModelAndView radioDischarge(@PathVariable(value = "deviceNo") String deviceNo) {
		ModelAndView view = new ModelAndView("Sampling/samplingList");
		
		String sampleType = "discharge";
		String url = "/sampling/" + sampleType + "/samplingListData";
		String title = "排放口采样记录";
		view.addObject("columns", CommonPage.setColumnToPage(null, null, url, title));
		view.addObject("deviceNo", deviceNo);
		view.addObject("title", title);
		return view;
	}
//企业雨水排放口按钮
	@RequestMapping(value = "/radioRainwater/{deviceNo}")
	public ModelAndView radioRainwater(@PathVariable(value = "deviceNo") String deviceNo) {
		ModelAndView view = new ModelAndView("Sampling/samplingList");
		
		String sampleType = "rainwater";
		String url = "/sampling/" + sampleType + "/samplingListData";
		String title = "企业雨水排放口采样记录";
		view.addObject("columns", CommonPage.setColumnToPage(null, null, url, title));
		view.addObject("deviceNo", deviceNo);
		view.addObject("title", title);
		return view;
	}

	//公共雨水排放口按钮
	@RequestMapping(value = "/radioStormwater/{deviceNo}")
	public ModelAndView radioStormwater(@PathVariable(value = "deviceNo") String deviceNo) {
		ModelAndView view = new ModelAndView("Sampling/samplingList");
		
		String sampleType = "stormwater";
		String url = "/sampling/" + sampleType + "/samplingListData";
		String title = "公共雨水排放口采样记录";
		view.addObject("columns", CommonPage.setColumnToPage(null, null, url, title));
		view.addObject("deviceNo", deviceNo);
		view.addObject("title", title);
		return view;
	}
	
	//废气排放口按钮
	@RequestMapping(value = "/radioWastegas/{deviceNo}")
	public ModelAndView radioSastegas(@PathVariable(value = "deviceNo") String deviceNo) {
		ModelAndView view = new ModelAndView("Sampling/samplingList");
		
		String sampleType = "wastegas";
		String url = "/sampling/" + sampleType + "/samplingListData";
		String title = "废气排放口采样记录";
		view.addObject("columns", CommonPage.setColumnToPage(null, null, url, title));
		view.addObject("deviceNo", deviceNo);
		view.addObject("title", title);
		return view;
	}

}
